package com.hl.sfzy.service;

import com.hl.sfzy.entity.LogonUser;
import com.hl.sfzy.mapper.LogonUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: sfzy
 * @description: 孙源源-管理员登录
 * @author: Syy
 * @create: 2024-09-06 15:50
 **/
@Service
public class LogonAdminService {
    @Autowired
    private LogonUserMapper logonUserMapper;

    //登录业务
    public int logon(String identifier, String password) {
        //判断identifier是用户名还是手机号
        boolean isPhone = identifier.matches("\\d{10,11}");
        //通过用户名或者手机号查询数据库
        LogonUser user = isPhone ? logonUserMapper.findLogonUserByPhone(identifier) : logonUserMapper.findLogonUserByUsername(identifier);
        System.out.println(user);

        //业务逻辑
        if (user == null) {
            //用户名不存在
            return -1;// 登录失败
        } else if (user.getRole() != 1) {
            //判断role不为1(不是用户),与用户名不存在一个道理
            return -1;// 登录失败
        } else if (user.getPassword().equals(password)) {
            return 1; // 登录成功
        }
        //密码错误
        return 0; // 登录失败
    }
}
